/**
* <p>Title: MailToLeaders.java</p>
* <p>Description: </p>
* <p>Company: www.huoyunren.com</p>
* @author Lanji
* @date 2015年9月16日
* @version 1.0
*/
package com.hxbd.test.utils.SendMails;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;

import com.duan.test.utils.SendMails.AbstractMailSender;
import com.duan.test.utils.SendMails.SimpleMailSender;
import com.duan.test.utils.common.CommonUtils;
/**
 * 发送邮件给leader组
 */
public class MailToLeaders {
	public static void main(String[] args) throws MessagingException {
		SimpleMailSender ss = new SimpleMailSender();
		Properties props = new Properties();
		InputStream inputStream = AbstractMailSender.class.getClassLoader().
				getResourceAsStream("mail.properties");
		try {
			props.load(inputStream);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if (inputStream != null) {
					inputStream.close();
				}
			} catch (IOException e) {
			}
		}
        // 协议名称设置为smtps，会使用SSL  
        final String user = props.getProperty("mail.smtp.user");
        final String pwd = props.getProperty("mail.smtp.password");
		Authenticator authenticator = new Authenticator() {
			@Override
			protected PasswordAuthentication getPasswordAuthentication() {
				return new PasswordAuthentication(user, pwd);
			}
		};
		ss.createMail(props,authenticator);
		ss.setFrom(props.getProperty("mail.smtp.from"));
		List<String> tos = new ArrayList<String>();
		String to[] = props.getProperty("tos").split(",");
		for (String t : to) {
			tos.add(t);
		}
		ss.setTo(tos);
		List<String> ccs = new ArrayList<String>();
		String cc[] = props.getProperty("ccs").split(",");
		for (String c : cc) {
			ccs.add(c);
		}
		ss.setCc(ccs);
		ss.setSubject("G7S_autotest_report_Product");		
		BufferedReader reader = null;
		String html = "";
		try {
			reader = new BufferedReader(new InputStreamReader(
					new FileInputStream(new File(CommonUtils.getFilePath("//test-output//NewReport.html"))), "utf-8"));
			String line = null;
			while ((line = reader.readLine()) != null) {
				html = html + line + "<br>";
			}
			ss.addHtml(html);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if(reader != null){
				try {
					reader.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		try {
			ss.sendMail();
		} catch (MessagingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
